package com.zte.sys.login.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.sys.login.dao.UserLoginDao;
import com.zte.sys.login.model.*;
import com.zte.sys.login.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    private UserLoginDao userLoginDao;

    @Override
    public LUsers detailOfUser(LUsers user) {
        LUsers users = userLoginDao.detailOfUser(user);
        return users;
    }

    @Override
    public List<Module> detailOfModule(List<Permission> permissionList) {
        List<Module> list = new ArrayList<>();
        for(int i=0;i<permissionList.size();i++){
            Permission permission = permissionList.get(i);
            Module module = userLoginDao.detailOfModules(permission);
            list.add(module);
        }
        return list;
    }

    @Override
    public String getModuleOfId(String module) {
        String moduleOfId = userLoginDao.getModuleOfId(module);
        return moduleOfId;
    }

    @Override
    @Transactional
    public void insertJournal(Journal journal) {
        userLoginDao.insertJournal(journal);
    }

    @Override
    public PageInfo<Journal> queryLogs(int pageNum, QueryCondition queryCondition, String uname) {
        if(queryCondition.getUId().length()==0){
            if(uname!=""){
                String uId = userLoginDao.getUserOfId(uname);
                queryCondition.setUId(uId);
            }
        }
        if(uname!=""){
            String uId = userLoginDao.getUserOfId(uname);
            if(!queryCondition.getUId().equals(uId)){
                return null;
            }
        }
        PageHelper.startPage(pageNum,5);
        List<Journal> journals = userLoginDao.queryLogs(queryCondition);
        PageInfo<Journal> pageInfo = new PageInfo<>(journals,10);
        return pageInfo;
    }
}
